package com.easylinkin.linkappapi.circuit.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.circuit.dto.AlarmTypeCount;
import com.easylinkin.linkappapi.circuit.dto.RailGiveSystemAlarmDTO;
import com.easylinkin.linkappapi.circuit.entity.RailGiveSystemAlarm;
import com.easylinkin.linkappapi.circuit.vo.RailGiveSystemAlarmVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface RailGiveSystemAlarmMapper extends  BaseMapper<RailGiveSystemAlarm> {

    IPage<RailGiveSystemAlarmVO> findPage(Page<RailGiveSystemAlarm> page, @Param("entity") RailGiveSystemAlarmDTO dto);

    List<RailGiveSystemAlarmVO> findPage(@Param("entity") RailGiveSystemAlarmDTO dto);

    @Select("SELECT COUNT(1) FROM rail_give_system_alarm a WHERE a.tenant_id=#{tenantId} and a.create_time like concat(#{date},'%') and a.category=#{category}")
    int amountByOneDate(@Param("tenantId")String tenantId,@Param("date")String date,@Param("category")String category);


    @Select("SELECT COUNT(1) FROM rail_give_system_alarm a WHERE a.tenant_id=#{tenantId} and a.category=#{category}")
    int amountByOneTenantId(@Param("tenantId")String tenantId,@Param("category")String category);


    @Select("SELECT COUNT(1) FROM rail_give_system_alarm a WHERE a.tenant_id=#{tenantId} and a.level =#{level} and a.category=#{category}")
    int amountByOneLevel(@Param("tenantId")String tenantId,@Param("level")String level,@Param("category")String category);

    @Select("SELECT COUNT(1) FROM rail_give_system_alarm a WHERE a.tenant_id=#{tenantId} and a.category=#{category} and a.status = #{status}")
    int amountByOneTenantIdAndStatus(@Param("tenantId")String tenantId, @Param("category")String category, @Param("status")int status);

    /**
     * 只查询当前时间前一天的AI识别主机告警未处理的数据
     * @return
     */
    @Select("SELECT a.*  FROM rail_give_system_alarm a WHERE a.create_time BETWEEN  DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY),'%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 23:59:59') and " +
            "a.status=0 and a.alarm_type in('24','25','26','27','28','29','30','31','32','33') ")
    List<RailGiveSystemAlarm> selectByOldDays();

    @Select("SELECT COUNT(1) FROM rail_give_system_alarm a where a.tenant_id=#{tenantId} and (DATE_FORMAT(a.create_time,'%Y-%m-%d') >= #{startDate} and DATE_FORMAT( a.create_time,'%Y-%m-%d') <= #{endDate}) and a.category=#{category} ")
    int amountByOneDateInterval(@Param("tenantId")String tenantId,@Param("startDate") String startDate, @Param("endDate") String dayDate, @Param("category") String category);

    List<AlarmTypeCount> alarmTypeCount(@Param("tenantId") String tenantId, @Param("category") String category, @Param("typeList") List<String> typeList, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
